from fastapi import APIRouter, HTTPException

from app.lib import yolo
from app.service import PhotoService
from app.response import ResponseModel
from app.model import Photo, PhotoResponse, PhotoSimpleResponse

router = APIRouter(
  prefix="/photo",
  tags=["photo"],
  responses={404: {"msg": "not found"}},
)


@router.post("/yolo")
async def yolo_test(photo: Photo):
  return yolo.get_summary(photo.base64)


@router.post("/standardized")
async def standardized(photo: Photo):
  photo_service = PhotoService(photo=photo)
  bs64, warnning = photo_service.photo_standardized_only()
  res = PhotoSimpleResponse(
    base64=bs64,
    warnning=warnning
  )
  return ResponseModel[PhotoSimpleResponse](data=res)


@router.post("/standardized-check", response_model=ResponseModel[PhotoResponse])
async def standardized(photo: Photo):
  photo_service = PhotoService(photo=photo)
  bs64_seg, bs64_unscaled, warnning = photo_service.photo_standardized()
  bs64_mark = photo_service.photo_mark()
  config = photo_service.final_parameter()
  res = PhotoResponse(
    config=config,
    base64=bs64_seg,
    base64_unscaled=bs64_unscaled,
    base64_mark=bs64_mark,
    warnning=warnning
  )
  return ResponseModel[PhotoResponse](data=res)

# class Item(BaseModel):
#   yoloId: int
#
# @router.get("/yolo/{id}", response_model=ResponseModel[Item])
# async def yolo(id: int):
#   if (id == 1):
#     raise BaseException(status_code=501, detail="数据未找到")
#   if (id == 2):
#     raise HTTPException(status_code=400, detail="http自己的异常")
#   r = Item(yoloId=id)
#   return ResponseModel[Item](data=r)
